<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/static/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>调账</title>
<base href="<%=basePath%>">

<link rel="stylesheet" href="static/ace/css/ace.css" class="ace-main-stylesheet" id="main-ace-style" />
<script type="text/javascript" src="../plugins/websocketInstantMsg/ext4/ext-all-debug.js"></script>
<script type="text/javascript" src="static/js/jquery-1.7.2.js" ></script>
<script type="text/javascript" src="../plugins/websocketInstantMsg/ext4/locale/ext-lang-zh_CN.js"></script>
<link rel="stylesheet" type="text/css" href="../plugins/websocketInstantMsg/ext4/resources/css/ext-all.css" />
</head>
<body>
<div id="combox"></div>
</body>
<script type="text/javascript">
Ext.onReady(function(){
	var store = Ext.create('Ext.data.Store', {
		fields: [	"ID",
		         	"COST_STATUS",
		         	"BILL_CODE",
		        	"LADING_BILL_CODE",
		        	"BILL_TYPE",
		        	"DINGCANG",
		        	"SETTLEMENT_UNIT",
		        	"COST_NAME",
		        	"CONTAINER_TYPE",
		        	"TAX_PRICE",
		        	"NON_TAX_PRICE",
		        	"TAX_RATE",
		        	"CONTAINER_NUM",
		        	"FEW",
		        	"TOTALAMOUNT",
		        	"CURRENCY",
		        	"EXCHANGE_RATE",
		        	"SHIP_NAME",
		        	"SHIP_VOYAGE_NAME",
		        	"SHIP_ATD",
		        	"BOAT_NAME",
		        	"BOAT_VOYAGE_NAME",
		        	"BOAT_ATD",
		        	"KEFU",
		        	"SALER_NAME",
		        	"REMARK"],
		pageSize: 25,  //页容量5条数据
		//是否在服务端排序 （true的话，在客户端就不能排序）
		proxy: {
			type: 'ajax',
			url: '../account/findInfo?state='+"",
			reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
				type: 'json', //返回数据类型为json格式
				 root: 'result.list',  //数据
	                totalProperty: 'result.total' //数据总条数
			}
		},
		autoLoad: true
	});
	var store1 = Ext.create('Ext.data.Store', {
		fields: ["Fee"],
		pageSize: 30,  //页容量5条数据
		//是否在服务端排序 （true的话，在客户端就不能排序）
		proxy: {
			type: 'ajax',
			url: '../account/findFeeNameInfo',
			reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
				type: 'json', //返回数据类型为json格式
				root: 'root1',  //数据
				totalProperty: 'total' //数据总条数
			}
		},
	});
	var store2 = Ext.create('Ext.data.Store', {
		fields: ["COST_NAME","TAX_RATE","COST_ITEM"],
		pageSize: 30,  //页容量5条数据
		//是否在服务端排序 （true的话，在客户端就不能排序）
		proxy: {
			type: 'ajax',
			url: '../account/findFeeProject',
			reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
				type: 'json', //返回数据类型为json格式
				root: 'root2',  //数据
				totalProperty: 'total' //数据总条数
			}
		},
	});
	var tradeTypeStore = Ext.create('Ext.data.Store', {
		fields: ['id', 'name'],
		data : [
		     {"id":"","name":"请选择"},
			{"id":"1", "name":"收款"},
			{"id":"0", "name":"付款"}
		]
	});
	var shuilv = Ext.create('Ext.data.Store', {
		fields: ['id', 'name'],
		data : [
		     {"id":0,"name":"0%"},
			{"id":"0.03", "name":"3%"},
			{"id":"0.06", "name":"6%"},
			{"id":"0.11", "name":"11%"},
			{"id":"0.17", "name":"17%"},
		]
	});
	var TypeCombo = new Ext.form.ComboBox({
		 id:'TypeCombo',
		fieldLabel:'费用类型',
		store: tradeTypeStore,
		displayField:'name',
		valueField:'id',
		typeAhead: false, 
		mode: 'local',
		allowBlank:false,
		triggerAction: 'all',
		selectOnFocus:true,
		forceSelection:true,
		editable:false,
		validateOnBlur:false,
		anchor:'95%',
		emptyText:'请选择...',
// 		renderTo:"combox",
		listeners:{

			   select:function(combo,record,opts) { 
				if(record[0].get("id")==1){
					//收款
						 var store = Ext.getCmp('grid').getStore();
	    				store.on('beforeload', function(store,options) {  
	    			            var new_params1 = {  
	    			            		state : record[0].get("id")
	    			            };  
	    				//这里apply一下实现新的proxy  
	    			            Ext.apply(store.proxy.extraParams,new_params1);  
	    			        });  
	    					store.load();  

				}else if(record[0].get("id")==0){
					//付款
					var store = Ext.getCmp('grid').getStore();
    				store.on('beforeload', function(store,options) {  
    			            var new_params1 = {  
    			            		state : record[0].get("id")
    			            };  
    				//这里apply一下实现新的proxy  
    			            Ext.apply(store.proxy.extraParams,new_params1);  
    			        });  
    					store.load(); 
				}else{
					//如果为空则展示所有数据
					var store = Ext.getCmp('grid').getStore();
    				store.on('beforeload', function(store,options) {  
    			            var new_params1 = {  
    			            		state : ""
    			            };  
    				//这里apply一下实现新的proxy  
    			            Ext.apply(store.proxy.extraParams,new_params1);  
    			        });  
    					store.load(); 
				}

			   } 

			  } 
	});
	var bs = Ext.create('Ext.container.Container', {
		layout: 'column',
		items:[TypeCombo]
	});
	  var selForm = Ext.create('Ext.form.Panel', {
		  title:"费用类型",
			name: 'selForm',
			renderTo: "combox",
			width:'100%',
//			collapsed :true,
			height:60,
//			collapsible:true,
			bodyStyle: {
				padding: '5px',
				border: 1
			},
			fieldDefaults: {
				labelAlign: 'left'
			},
			items: [bs]
		});
	  var apply =new Ext.Button({
			text: '调账',
	     	listeners: {
	         'click': function () {
	        	 var records = grid.getSelectionModel().getSelection();
	        	 if(records.length==0){
		       			Ext.Msg.alert("提示","请选择一条记录调账");return;
		       		}
		       		if(records.length>1){
		       			Ext.Msg.alert("提示","只能选择一条记录调账");return;
		       		}
					 
			
    				
		       		var win = Ext.create("Ext.window.Window", {
		 				title: "编辑",
		 				modal:true,
		 				/*width: 800,
		 				height: 360,*/
		 				maximizable: true,
		 				closeAction:'destroy',
		 				layout: "fit",
//		  				margin: '3 3 0',
		 		        buttonAlign:'center',
		 				items: [{
				        		xtype: "form",
					        		id:'form',
					        		bodyPadding: 5,
					        		layout: "hbox",
					        		defaults: {
					        			border: true,
					        			flex: 1,
					        			anchor: '100%'
					        		},
					        		fieldDefaults: {
					        			labelWidth: 30,
//	 				        			labelAlign: "right",
					        			//flex: 1,
					        			margin: 5,
					        			labelStyle: 'color:green;'
					        		},
					        		items: [{
	   					        		xtype: 'textfield',
	   					        		name: 'id',
	   					        		hidden: true, 
	   					        		hideLabel:true,
	   					        		value:records[0].data.ID,
	   					        	},{
										xtype: "combobox",
										id:"COST_NAME",
       					        		name: 'COST_NAME',
       					        		fieldLabel: '费用名称',
       					        		emptyText: "--请选择--",
       					        		displayField: "COST_NAME",
       					        		valueField:'COST_ITEM',
//        					        		hiddenName:'TAX_RATE',
       					        		store:store2,
   					        		listeners: {
  					    	         'select': function () {
  					    	        	Ext.getCmp('FEE_PROJECT').setValue(Ext.getCmp("COST_NAME").value ); 
  					    	        	var tt = Ext.getCmp("COST_NAME").valueModels[0].raw.TAX_RATE
  					    	        	var UNIT_PRICE = Ext.getCmp('UNIT_PRICE').getValue();
  					    	        	var cc = tt.toFixed(4);
  					    	        	Ext.getCmp('TAX_RATE').setValue(cc.slice(2,4)+"."+cc.slice(4,6)+"%");
  					    	        	Ext.getCmp('NON_TAX_PRICE').setValue(UNIT_PRICE/(1+tt));
  					    	        	
  					    	         }
  					        		}
       					        	},{
										xtype: "combobox",
										id:"TAX_RATE",
       					        		name: 'TAX_RATE',
       					        		fieldLabel: '税率',
       					        		emptyText: "--请选择--",
       					        		displayField: "name",
       					        		valueField:'id',
       					        		store:shuilv,
   					        		listeners: {
  					    	         'select': function () { 
  					    	        	
  					    	        	var tt = Ext.getCmp("TAX_RATE").value
  					    	        	alert(tt);
  					    	        	var UNIT_PRICE = Ext.getCmp('UNIT_PRICE').getValue();
  					    	        	alert(UNIT_PRICE);
  					    	        	Ext.getCmp('NON_TAX_PRICE').setValue(UNIT_PRICE/(1+parseFloat(tt)));
  					    	         }
  					        		}
       					        	},
       					        	{
										xtype: "textfield",
										id:'FEE_PROJECT',
       					        		name: 'FEE_PROJECT',
       					        		fieldLabel: '费用项目',
       					        		

       					        	},{
	   					        		xtype: 'textfield',
	   					        		id:'UNIT_PRICE',
	   					        		name: 'UNIT_PRICE',
	   					        		fieldLabel: '含税价',
	   					        		store:store1,
	   					        	 listeners:{
	   					              blur: function(){
	   					            	var TAX_RATE = Ext.getCmp("COST_NAME").valueModels[0].raw.TAX_RATE;
	   					            	var UNIT_PRICE = Ext.getCmp('UNIT_PRICE').getValue();
	   					            	Ext.getCmp('NON_TAX_PRICE').setValue(UNIT_PRICE/(TAX_RATE+1));
	   					              } 
	   					        	 }
	   					        	},

	   					        	{
	   					        		xtype: 'textfield',
	   					        		id:'NON_TAX_PRICE',
	   					        		name: 'NON_TAX_PRICE',
	   					        		fieldLabel: '不含税价'
	   					        	}]
		 				}],
		       	 buttons: [{
	 		            text: '确定',
	 		            handler: function(){
	 		            	alert(records[0].data.COST_NAME);
	 		            	alert(Ext.getCmp('TAX_RATE').getValue());
	 		            	var form = Ext.getCmp('form').getForm();
	 		            	Ext.Ajax.request({
	       						url:'../account/saveInfo',
	       						params:{
	       							ID:encodeURI(records[0].data.ID),
	       							COST_NAME:encodeURI(records[0].data.COST_NAME),
	 		            			TAX_RATE:encodeURI(records[0].data.TAX_RATE),
	 		            			ADJUST_TAX_RATE:Ext.getCmp('TAX_RATE').getValue(),
	 		            			COST_ITEM:encodeURI(Ext.getCmp('FEE_PROJECT').getValue()),
	 		            			TAX_PRICE:encodeURI(records[0].data.TAX_PRICE),
	 		            			ADJUST_TAX_PRICE:encodeURI(Ext.getCmp('UNIT_PRICE').getValue()),
	 		            			NON_TAX_PRICE:encodeURI(records[0].data.NON_TAX_PRICE),
	 		            			ADJUST_NON_TAX_PRICE:encodeURI(Ext.getCmp('NON_TAX_PRICE').getValue()),
	 		            			BILL_CODE:encodeURI(records[0].data.BILL_CODE),
	 		            			LADING_BILL_CODE:encodeURI(records[0].data.LADING_BILL_CODE),
	 		            			COST_PLATE:encodeURI(records[0].data.COST_PLATE),
	 		            			COST_NAME:encodeURI(records[0].data.COST_NAME),
	 		            			CURRENCY:encodeURI(records[0].data.CURRENCY),
	 		            			SETTLEMENT_UNIT:encodeURI(records[0].data.SETTLEMENT_UNIT),
	 		            			CONTAINER_TYPE:encodeURI(records[0].data.CONTAINER_TYPE),
	 		            			CONTAINER_NUM:encodeURI(records[0].data.CONTAINER_NUM),
	 		            			STATE:"1"
	       						},
	       						timeout:5000,
	       						success:function(response){
	       							store.load();
	       							win.close();
	       							Ext.Msg.alert("成功","保存成功!");
	       						},
	       						failure:function(response){
	       							store.load();
	       							win.close();
	       							Ext.Msg.alert("成功","保存成功!");
	       						}
	       					});
// 	 		            	form.submit({
// 	 		            		params:{
	 		            			
// 	 		            		},
// 	 		            		waitTitle : '请稍后...',  
// 	 		            		waitMsg : '正在保存信息,请稍后...',  
// 	 		            		url:'../account/saveInfo',
// 		          				success:function(form,action){
// 		          					Ext.Msg.alert("提示","保存成功");
// 		          					store.load();
// 		          					store2.load();
// 		          					win.close();
// 		          				},

// 		          			});
	 		            }
	 		        },{
	 		            text: '关闭',
	 		            handler: function(){
	 		            	win.close();
	 		            }
	 		        }]
		       		});
		       		
		       		win.show();
    				store1.on('beforeload', function(store,options) {  
    					
			            var new_params1 = {  
			            		BILL_CODE:encodeURI(records[0].data.BILL_CODE),
			            		COST_NAME:encodeURI(records[0].data.COST_NAME)
			            };  
				//这里apply一下实现新的proxy  
			            Ext.apply(store.proxy.extraParams,new_params1);  
			        });  
					store1.load({callback :function(r,options,success){  
						var tt= store1.getAt(0).get('Fee');
						Ext.getCmp("UNIT_PRICE").setValue(tt);
				    }  }); 
	store2.on('beforeload', function(store,options) {  
    					
			            var new_params1 = {  
// 			            		BILL_CODE:encodeURI(records[0].data.BILL_CODE),
			            		COST_NAME:encodeURI(records[0].data.COST_NAME)
			            };  
				//这里apply一下实现新的proxy  
			            Ext.apply(store.proxy.extraParams,new_params1);  
			        });  
					store2.load();
					
	         }
	     	}
		});
	var grid = new Ext.grid.GridPanel({
		//collapsible :true,
	    //split: true,
	    title:"调账",
	    height: document.documentElement.clientHeight-25,
	    width:2000,
	    store:store,
	    forceFit:true,
	    id:'grid',
 		selModel:Ext.create('Ext.selection.CheckboxModel',{checkOnly:false}), 
	    columnLines: true,
	    menuDisabled:true,
	    bbar: [{
			xtype: 'pagingtoolbar',
			store: store,
			displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
			emptyMsg: "没有数据",
			beforePageText: "当前页",
			afterPageText: "共{0}页",
			displayInfo: true
		}],
	     tbar: [apply],
	    viewConfig:{enableTextSelection:true,stripeRows: true},
	    columns: [
					{text: 'ID',  dataIndex:'ID',hidden:true,maxWidth: 200},
					{text: '费用状态',  dataIndex:'COST_STATUS',hidden:true,maxWidth: 200},
					{text: '托单号',  enableColumnResize:true,dataIndex:'BILL_CODE', maxWidth: 300 ,},
					{text: '提单号',  dataIndex:'LADING_BILL_CODE',enableColumnResize:true,maxWidth: 200},
					{text: '提单类型',  dataIndex:'BILL_TYPE',maxWidth: 80},
					{text: '订舱单位',  dataIndex:'DINGCANG',maxWidth: 80},
					{text: '结算单位', dataIndex:'SETTLEMENT_UNIT', minWidth: 80},
					{text: '费用名称',  dataIndex:'COST_NAME',maxWidth: 80},
					{text: '箱型',  dataIndex:'CONTAINER_TYPE',maxWidth: 80},
					{text: '含税价',  dataIndex:'TAX_PRICE',maxWidth: 80},
					{text: '不含税价',  dataIndex:'NON_TAX_PRICE',maxWidth: 80},
					{text: '税率',  dataIndex:'TAX_RATE',maxWidth: 80},
					{text: '箱量', dataIndex:'CONTAINER_NUM', maxWidth: 80},
					{text: '总金额', dataIndex:'TOTALAMOUNT', maxWidth: 80},
					{text: '币种',  dataIndex:'CURRENCY',maxWidth: 80},
					{text: '汇率',  dataIndex:'EXCHANGE_RATE',maxWidth: 80},
					{text: '海船名',  dataIndex:'SHIP_NAME',maxWidth: 80},
					{text: '海船航次',  dataIndex:'SHIP_VOYAGE_NAME',maxWidth: 80},
					{text: '海船实际开航时间',  dataIndex:'SHIP_ATD',maxWidth: 80},
					{text: '江船名',  dataIndex:'BOAT_NAME',maxWidth: 80},
					{text: '江船航次',  dataIndex:'BOAT_VOYAGE_NAME',maxWidth: 80},
					{text: '江船开航日期', dataIndex:'BOAT_ATD', maxWidth: 80},
					{text: '客服', dataIndex:'KEFU', maxWidth: 80},
					{text: '销售',  dataIndex:'SALER_NAME',maxWidth: 80},
					{text: '录入人',  dataIndex:'BOX_TYPE_ABBREVIATION',maxWidth: 80},
					{text: '录入日期',  dataIndex:'BOX_TYPE_ABBREVIATION',maxWidth: 80},
					{text: '备注', dataIndex:'REMARK', maxWidth: 80},
		
			
			],
	    renderTo:Ext.getBody(),
	});

// 	Ext.create("Ext.Panel",{
// 				title:"调账",
// 				width:"100%", 
// 		          height:700, 
		     
// 					frame:true,    //渲染面板 
// 		          defaults : {//设置默认属性 
// 		              bodyStyle:'background-color:#FFFFFF;',//设置面板体的背景色 
// 		              frame : true   
// 		          }, 
// 				items:[TypeCombo,grid]
// 	});
});

</script>
</html>